<template>
  <div class="favorite-card" :class="{ 'not-favorite': !item.isFavorite }" @click="handleCardClick">
    <!-- 封面图片 -->
    <div v-if="item.imageUrl" class="card-image">
      <img :src="item.imageUrl" :alt="item.title" @error="handleImageError" />
      <div class="image-overlay">
        <el-icon class="type-icon">
          <component :is="getTypeIcon(item.type)" />
        </el-icon>
      </div>
    </div>

    <!-- 卡片内容 -->
    <div class="card-content">
      <!-- 标题和收藏状态 -->
      <div class="card-header">
        <h3 class="card-title" :title="item.title">{{ item.title }}</h3>

        <!-- 操作按钮 -->
        <div class="card-actions" @click.stop>
          <el-button size="small" type="primary" @click="$emit('edit', item)">编辑</el-button>
          <el-button size="small" type="danger" @click="$emit('delete', item)">删除</el-button>
        </div>
      </div>

      <!-- 描述 -->
      <p v-if="item.description" class="card-description">{{ item.description }}</p>
      
      <!-- 内容预览（文本类型） -->
      <p v-if="item.content && item.type === 'text'" class="card-content-preview">
        "{{ truncateText(item.content, 100) }}"
      </p>
      
      <!-- 链接预览（链接类型） -->
      <div v-if="item.url && item.type === 'link'" class="card-url">
        <el-icon><Link /></el-icon>
        <span class="url-text">{{ formatUrl(item.url) }}</span>
      </div>

      <!-- 标签 -->
      <div v-if="item.tags && item.tags.length > 0" class="card-tags">
        <el-tag
          v-for="tag in item.tags"
          :key="tag"
          size="small"
          type="info"
          class="tag-item"
        >
          {{ tag }}
        </el-tag>
      </div>

      <!-- 元信息 -->
      <div class="card-meta">
        <div class="meta-left">
          <span v-if="item.source" class="meta-item">
            <el-icon><Platform /></el-icon>
            {{ item.source }}
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import {
  Document,
  Link,
  Picture,
  VideoPlay,
  Platform
} from '@element-plus/icons-vue'
import type { FavoriteItem } from '../types'

interface Props {
  item: FavoriteItem
}

interface Emits {
  (e: 'view', item: FavoriteItem): void
  (e: 'edit', item: FavoriteItem): void
  (e: 'delete', item: FavoriteItem): void
  (e: 'toggle-favorite', item: FavoriteItem): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

// 处理卡片点击
const handleCardClick = () => {
  emit('view', props.item)
}

// 获取类型图标
const getTypeIcon = (type: string) => {
  switch (type) {
    case 'text':
      return Document
    case 'link':
      return Link
    case 'image':
      return Picture
    case 'video':
      return VideoPlay
    default:
      return Document
  }
}

// 截断文本
const truncateText = (text: string, maxLength: number) => {
  if (text.length <= maxLength) return text
  return text.substring(0, maxLength) + '...'
}

// 格式化URL显示
const formatUrl = (url: string) => {
  try {
    const urlObj = new URL(url)
    return urlObj.hostname
  } catch {
    return url.length > 30 ? url.substring(0, 30) + '...' : url
  }
}

// 处理图片加载错误
const handleImageError = (event: Event) => {
  const img = event.target as HTMLImageElement
  img.style.display = 'none'
}
</script>

<style scoped>
.favorite-card {
  background: var(--el-bg-color);
  border: 1px solid var(--el-border-color-light);
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.2s ease;
  height: 100%;
  display: flex;
  flex-direction: column;
  cursor: pointer;
}

.favorite-card:hover {
  border-color: var(--el-color-primary);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.favorite-card.not-favorite {
  opacity: 0.7;
}

.card-image {
  position: relative;
  height: 160px;
  overflow: hidden;
}

.card-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.image-overlay {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(0, 0, 0, 0.6);
  border-radius: 4px;
  padding: 4px;
}

.type-icon {
  color: white;
  font-size: 16px;
}

.card-placeholder {
  height: 120px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--el-fill-color-light);
  gap: 8px;
}

.placeholder-icon {
  font-size: 32px;
  color: var(--el-text-color-placeholder);
}

.type-text {
  font-size: 14px;
  color: var(--el-text-color-regular);
}

.card-content {
  padding: 16px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
}

.card-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--el-text-color-primary);
  margin: 0;
  flex: 1;
  line-height: 1.4;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  margin-right: 8px;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.card-description {
  font-size: 14px;
  color: var(--el-text-color-regular);
  line-height: 1.5;
  margin: 0 0 12px 0;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.card-content-preview {
  font-size: 14px;
  color: var(--el-text-color-secondary);
  line-height: 1.5;
  margin: 0 0 12px 0;
  font-style: italic;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.card-url {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 12px;
  padding: 8px;
  background: var(--el-fill-color-light);
  border-radius: 4px;
}

.url-text {
  font-size: 14px;
  color: var(--el-text-color-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.tag-item {
  font-size: 12px;
}

.card-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  margin-top: auto;
}

.meta-left, .meta-right {
  display: flex;
  gap: 12px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--el-text-color-placeholder);
}

.card-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.favorite-card:hover .card-actions {
  opacity: 1;
}

/* 深色模式适配 */
.dark .favorite-card {
  background: var(--el-bg-color-overlay);
  border-color: var(--el-border-color);
}

.dark .card-placeholder {
  background: var(--el-fill-color);
}

.dark .card-url {
  background: var(--el-fill-color);
}
</style>